import co from 'co'
let OSS = require('ali-oss');

const OSSConfig = {
uploadHost: 'http://lw-sit.oss-cn-beijing.aliyuncs.com', //OSS上传地址
type: 'scs',
ossParams: {
  region: 'oss-cn-beijing',
  // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
  accessKeyId: 'LTAI5tCLKRXwiuQXaKN19XDr',
  accessKeySecret: 'byuXwUpEEuMAaUYYVP5X623ndD1hEN',
  bucket: 'lw-sit'
}
};

function uploadOSS(file, fileName,dir) {
return new Promise((resolve, reject) => {
  var client = new OSS({
    region: OSSConfig.ossParams.region,
    accessKeyId: OSSConfig.ossParams.accessKeyId,
    accessKeySecret: OSSConfig.ossParams.accessKeySecret,
    bucket: OSSConfig.ossParams.bucket,
  });
  let timeSmap=new Date().getTime()
  // console.log(timeSmap);
  
  let randomName = `${dir}${timeSmap}${fileName}`;
  co(function* () {
    var result = yield client.multipartUpload(randomName, file);
    resolve(`${OSSConfig.uploadHost}/${result.name}`);
  }).catch(function (err) {
    console.log(err);
    reject(err);
  });
});
}


export { uploadOSS }
